package javase.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * create by LiangYM @Date 2020/7/27 18:55
 **/
public class ThreadRunnableDemo {
    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("hello thread");
        }
    }

    static class MyRunnable implements Runnable {

        public void run() {
            System.out.println("hello runnable");
        }
    }

    static void testSleep() {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //暂时退出，返回到就绪状态
    static void testYield() {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("A" + i);
                if (i%10==0) Thread.yield();
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("-----B" + i);
                if (i%10==0) Thread.yield();
            }
        }).start();
    }

    //join 用来等待另一个线程的结束
    static void testJoin(){
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 =new Thread(() -> {
            try {
                System.out.println("我来插个队");
                t1.join();
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t3 =new Thread(() -> {

            for (int i = 0; i < 10; i++) {
                try {
                    t2.join();
                    System.out.println("B" + i);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });




        t1.start();
        t2.start();
        t3.start();
    }

    static class MyTask implements Runnable {
        private static int taskNum;

        public MyTask(int num) {
            this.taskNum = num;
        }

        @Override
        public void run() {
            System.out.println("正在执行task "+taskNum);
            try {
                Thread.currentThread().sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("task "+taskNum+"执行完毕");
        }
    }

    public static void main(String[] args) {
        //runnable
//        new Thread(() -> {
//            System.out.println(123);
//        }).start();

//        new Thread(new MyRunnable()).start();
//        new MyThread().start();
        //通过线程池启动线程

        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));

        for(int i=0;i<15;i++){
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
                    executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
        }
        executor.shutdown();

//        testYield();

        //测试join
//        testJoin();





    }

}
